﻿using CDMS.Common;
using DevExpress.LookAndFeel;
using DevExpress.Skins;
using DevExpress.UserSkins;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraSplashScreen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;

namespace CDMS
{
    static class Program
    {
        public static bool isConnected = true;
        public static string userName = "";
        public static string passWord = "";
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] arguments)
        {
            DevExpress.Utils.LocalizationHelper.SetCurrentCulture(arguments);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

            try
            {
                Globals.Application_SetCultureInfo(System.Threading.Thread.CurrentThread);
                Control.CheckForIllegalCrossThreadCalls = false;

                BonusSkins.Register();
                SkinManager.EnableFormSkins();
                UserLookAndFeel.Default.SetSkinStyle("DevExpress Style");

                SplashScreenManager.ShowForm(typeof(SplashScreen1), true, true);

                //Get connection String from SQLParameters.xml file.
                string conn = "";
                string serverParameters = "SQLParameters.xml";
                if (!System.IO.File.Exists(serverParameters)) return;
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load(serverParameters);
                }
                catch { }
                if (doc.DocumentElement.Name == "Parameters")
                {
                    string[] prm = doc.DocumentElement.InnerText.Split(new char[] { ';' });

                    if (prm != null)
                    {
                        if (prm.Length > 0)
                            conn += "Data Source=" + prm[0] + ";";
                        if (prm.Length > 1)
                            conn += "Initial Catalog=" + prm[1] + ";";

                        if (prm.Length > 2)
                        {
                            if (string.IsNullOrEmpty(prm[2]))
                                conn += "Integrated Security=True;";
                            else
                            {
                                conn += "User Id=" + prm[2] + ";";
                                if (prm.Length > 3)
                                    conn += "Password=" + prm[3] + ";";
                            }
                        }
                        
                    }
                }
                Globals.connectionString = conn;
                //

                //Test connection
                DBAccess db = new DBAccess();
                isConnected = db.TestConnection();
                //
            }
            catch
            {
                isConnected = false;
            }
            SplashScreenManager.CloseForm(false);
            Application.Run(new frmMain());
        }

        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            try
            {
                FormGlobals.Message_Error(e.Exception.Message, "Lỗi xảy ra");
            }
            catch (Exception ex)
            {
                FormGlobals.Message_Error(ex.Message, "Lỗi xảy ra");
            }
        }
    }
}
